import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class CsvReader {
	private AttributeHandler attributeHandler = new AttributeHandler();
	private final String MESSAGE_ERROR_GET_CONTENTS_FROM_CSV = "getContentsFromCSV function error!";
	
	public ArrayList<Task> read() {
		return getTasksFromCSV();
	}
	
	private ArrayList<Task> getTasksFromCSV() {
	    ArrayList<String> dataBufferList = getContentsFromCSV();
	    ArrayList<Task> listOfTasks = attributeHandler.convertStringToTask(dataBufferList);
	    return listOfTasks;
	}
	
	
	private ArrayList<String> getContentsFromCSV() {
	    ArrayList<String> contentArr = new ArrayList<String>();

	    try {
	      FileReader fileReader = new FileReader(FileManager.getFileName());
	      BufferedReader bufferedReader = new BufferedReader(fileReader);

	      String stringBuffer;
	      while ((stringBuffer = bufferedReader.readLine()) != null) {
	        contentArr.add(stringBuffer);
	      }

	      bufferedReader.close();
	    } catch (IOException e) {
	      System.out.println(MESSAGE_ERROR_GET_CONTENTS_FROM_CSV);
	    }

	    return contentArr;
	}
}